package com.tuyou.springboot.async;

import org.springframework.aop.framework.AopContext;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;

@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AsyncApp {

	public static void main(String[] args) {
		SpringApplication.run(AsyncApp.class, args);
	}

	@Async
	public void async() throws InterruptedException {
		Thread.sleep(3000);
		System.out.println("异步方法执行完");
	}

	/**
	 * 异步方式测试
	 */
	@PostConstruct
	public void test() throws InterruptedException {
		System.out.println("test start");
		AsyncApp asyncApp = (AsyncApp) AopContext.currentProxy();
		asyncApp.async();
		System.out.println("test end");
	}
}
